package com.tma.android.gpm.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.gm.api.GoogleMusicApi;
import com.android.gm.api.model.Song;
import com.tma.android.gpm.R;
import com.tma.android.gpm.common.ConstantCommon;
import com.tma.android.gpm.customview.carousel.CarouselView;
import com.tma.android.gpm.customview.carousel.CarouselViewHelper;
import com.tma.android.gpm.data.database.handler.SongsTableHandler;
import com.tma.android.gpm.data.modelview.GridViewModelView;

public class GPMRecentFragment extends Fragment{

	private CarouselView mView;
	private Paint mPaint = new Paint();
	private CarouselViewHelper mHelper;
	private Bitmap mGlossyOverlay;
	private Bitmap mBorder;
	private Resources res;
	private List<GridViewModelView> listModels;
	
	
	
	protected class LocalCarouselViewHelper extends CarouselViewHelper
	{

		private static final int PIXEL_BORDER =1;
		private DetailTextureParameters mDetailTextureParameters = new DetailTextureParameters(5.0f, 5.0f,3.0f,10.0f);
		
		
		public LocalCarouselViewHelper(Context context)
		{
			super(context);
		}
		public LocalCarouselViewHelper(Context context,
				CarouselView carouselView) {
			super(context, carouselView);
			// TODO Auto-generated constructor stub
		}
		@Override
		public DetailTextureParameters getDetailTextureParameters(int id) {
			// TODO Auto-generated method stub
			return mDetailTextureParameters;
		}
		@Override
		public void onCardSelected(int n) {
			// TODO Auto-generated method stub
			super.onCardSelected(n);
		}
		@Override
		public void onDetailSelected(int n, int x, int y) {
			// TODO Auto-generated method stub
			super.onDetailSelected(n, x, y);
		}
		@Override
		public void onCardLongPress(int n, int[] touchPosition,
				Rect detailCoordinates) {
			// TODO Auto-generated method stub
			super.onCardLongPress(n, touchPosition, detailCoordinates);
		}
		@Override
		public Bitmap getTexture(int id) {
			// TODO Auto-generated method stub
			Bitmap bitmap = Bitmap.createBitmap(ConstantCommon.TEXTURE_WIDTH,ConstantCommon.TEXTURE_HEIGHT,Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(bitmap);
			canvas.drawARGB(0, 0, 0, 0);
			mPaint.setColor(0x40808080);
			canvas.drawRect(2, 2, ConstantCommon.TEXTURE_WIDTH-2, ConstantCommon.TEXTURE_HEIGHT-2, mPaint);
			mPaint.setAntiAlias(true);
			mPaint.setColor(0xffffffff);
			
			canvas.drawBitmap(BitmapFactory.decodeResource(res, listModels.get(id).getBitmap()), null, new Rect(PIXEL_BORDER, PIXEL_BORDER, ConstantCommon.TEXTURE_WIDTH - PIXEL_BORDER, ConstantCommon.TEXTURE_HEIGHT- PIXEL_BORDER), mPaint);
			return bitmap;
		}
		@Override
		public Bitmap getDetailTexture(int id) {
			// TODO Auto-generated method stub
			Bitmap bitmap = Bitmap.createBitmap(ConstantCommon.DETAIL_TEXTURE_WIDTH, ConstantCommon.DETAIL_TEXTURE_HEIGHT,Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(bitmap);
			
			mPaint.setTextSize(20.0f);
			mPaint.setAntiAlias(true);
			
			canvas.drawText(listModels.get(id).getTitle(), 0, ConstantCommon.DETAIL_TEXTURE_HEIGHT/2-25, mPaint);
			mPaint.setTextSize(10.0f);
			canvas.drawText(listModels.get(id).getSubTitle(), 0, ConstantCommon.DETAIL_TEXTURE_HEIGHT/2, mPaint);
			
			return bitmap;
		}
		
		
	}
	
	
	private class AsyncList extends AsyncTask<Void, Void, Void>
    {

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			GoogleMusicApi.login(getActivity(), "an.ledotruong@gmail.com", "P@$4word");
			try {
				SongsTableHandler helper = new SongsTableHandler(getActivity());
			List<Song> listofSong =	GoogleMusicApi.getAllSongs(getActivity());
			for (Song song : listofSong) {
				if(helper.isExist(song))
				{
					helper.addItem(song);
				}
				else
				{
					helper.updateItem(song);
				}
			}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			for(int i = 0 ;i < ConstantCommon.TOTAL_CARDS ; i++)
			{
				listModels.add(new GridViewModelView(i, "Title " + i, "SubTitle " + i, R.drawable.ic_launcher));
			    mView.createCards(i + 1);
			}
			return null;
		}
    	
    }
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	
		
	}
	 @Override
	  public View onCreateView(LayoutInflater inflater, ViewGroup container,
	      Bundle savedInstanceState) {
	    View view = inflater.inflate(R.layout.gpm_recent_screen_layout, container, false);
	    GoogleMusicApi.getInstance(getActivity());
	    mView= (CarouselView) view.findViewById(R.id.carousel);
	    mView.getHolder().setFormat(PixelFormat.RGBA_8888);
	    mPaint.setColor(0xffffffff);
	    res = getResources();
	    /** Set Helper */
	    mHelper = new LocalCarouselViewHelper(getActivity());
	    mHelper.setCarouselView(mView);
	    
	    mView.setSlotCount(ConstantCommon.CARD_SLOTS);
	    mView.createCards(ConstantCommon.INCREMENTAL_ADD?1:ConstantCommon.TOTAL_CARDS);
	    mView.setVisibleSlots(ConstantCommon.SLOTS_VISIBLE);
	    mView.setStartAngle((float)-(2.0f*Math.PI*5/ConstantCommon.CARD_SLOTS));
	    
	    mBorder = BitmapFactory.decodeResource(res, R.drawable.border);
	    mView.setDefaultBitmap(mBorder);
	    mView.setLoadingBitmap(mBorder);
//	    mGlossyOverlay = BitmapFactory.decodeResource(res, R.drawable.beautiful_green_background);
	  
//	    mView.setBackgroundBitmap(mGlossyOverlay);
//	    mView.setBackgroundColor(.0f, 0.0f, 0.0f, 0);
	    mView.setRezInCardCount(3.0f);
	    mView.setVisibleDetails(ConstantCommon.VISIBLE_DETAIL_COUNT);
	    mView.setDragModel(CarouselView.DRAG_MODEL_PLANE);
	    
	    
	    listModels = new ArrayList<GridViewModelView>();
	    new AsyncList().execute();
	    
	    
	    
	    return view;
	  }

	  
}
